<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>

      <el-input
        v-model="params.carNumber"
        clearable
        placeholder="请输入内容"
        class="search-main"
      />
      <span class="search-label">车主姓名：</span>

      <el-input
        v-model="params.personName"
        clearable
        placeholder="请输入内容"
        class="search-main"
      />
      <span class="search-label">状态：</span>

      <el-select v-model="params.cardStatus">
        <el-option
          v-for="item in statusList"
          :key="item.value"
          :label="item.text"
          :value="item.value"
        />
      </el-select>

      <el-button type="primary" class="search-btn" @click="getCardList"
        >查询</el-button
      >
    </div>

    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <div>
        <el-button type="primary" @click="$router.push('/car/cardAdd')"
          >添加月卡</el-button
        >

        <el-button type="primary" @click="delCardList">批量删除</el-button>
        <!-- delCardList  -->
      </div>
      <div class="tip">
        <i class="el-icon-info"></i>
        <span>本园区共计 1486 个车位，月卡用户 0 人，车位占有率 0.00%</span>
      </div>
    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="list"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" :index="indexMethod" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column
          label="状态"
          prop="cardStatus"
          :formatter="formatStatus"
        />

        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text">续费</el-button>

            <el-button size="mini" type="text">查看</el-button>

            <el-button size="mini" type="text" @click="ediCard(scope.row.id)"
              >编辑</el-button
            >

            <el-button size="mini" type="text" @click="delCard(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="page-container">
      <!-- current-page:表示当前页码 -->
      <!-- page-sizess：可选择的每页的页容量 -->
      <!-- page-size：当前页的页容量  page-size的值通常和page-sizes的第一个值保持一致-->
      <!-- layout:分页的样式 -->
      <!-- size-change：页容量发生变化时，会触发这个事件 -->
      <!-- current-change： 页码发生变化时，会触发这个事件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 添加楼宇 -->
    <el-dialog title="添加楼宇" width="580px">
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
            <el-form-item label="楼宇名称" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
  
            <el-form-item label="楼宇层数" prop="floors">
              <el-input v-model="addForm.floors" />
            </el-form-item>
  
            <el-form-item label="在管面积" prop="area">
              <el-input v-model="addForm.area" />
            </el-form-item>
  
            <el-form-item label="物业费" prop="propertyFeePrice">
              <el-input v-model="addForm.propertyFeePrice" />
            </el-form-item>
  
          </el-form> -->
      </div>

      <template #footer>
        <el-button size="mini">取 消</el-button>

        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
//引入接口
import { getCardListAPI, delCardAPI, delCardListAPI } from "@/api/card";

export default {
  name: "Card", //组件名
  data() {
    return {
      params: {
        page: 1, //当前页码
        pageSize: 2, // 每页显示的数量
        carNumber: "", // 车牌号码搜索参数
        personName: "", // 车主姓名搜索参数
        cardStatus: null, // 月卡状态搜索参数
      },
      list: [], // 月卡列表数据
      total: 2,
      statusList: [
        {
          text: "全部",
          value: null,
        },
        {
          text: "可用",
          value: "0",
        },
        {
          text: "已过期",
          value: "1",
        },
      ],
      //全部删除
      selectedCardList: [],
    };
  },
  created() {
    this.getCardList(); // 在组件创建时调用获取月卡列表的方法
  },
  methods: {
    //编辑月卡
    ediCard(id) {
      this.$router.push({
        path: "/car/cardAdd",
        query: {
          id,
        },
      });
      // console.log(row);
    },

    //点击查询按钮
    search() {
      //重置页码
      this.params.page = 1;
      this.getCardList();
    },
    //计算序号
    indexMethod(index) {
      //当前页之前所有的数据+index+1
      return (this.params.page - 1) * this.params.pageSize + index + 1;
    },
    //格式化状态
    formatStatus(row, column, cellValue, index) {
      //   if(cellValue === 0){
      //     return'可用'
      //   }
      //   if(cellValue ===1){
      //     return '不可用'
      //   }
      const Map = {
        0: "可用",
        1: "不可用",
      };
      return Map[cellValue];
    },

    //读取月卡列表的方法
    async getCardList() {
      // 调用 API 获取月卡列表
      const res = await getCardListAPI(this.params);
      this.list = res.data.rows; // 将获取到的月卡列表数据赋值给 list
      this.total = res.data.total;
      console.log(res); // 打印 API 响应，用于调试
    },
    handleSizeChange(val) {
      this.params.pageSize = val;
      this.getCardList();
    },
    handleCurrentChange(val) {
      this.params.page = val;
      this.getCardList();
    },

    delCard(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          //调用删除接口
          // const res = await delCardAPI(id)
          // console.log(res);

          await delCardAPI(id);
          this.getCardList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleSelectionChange(rowList) {
      console.log(rowList);
      this.selectedCardList = rowList;
    },
    //批量删除月卡详情
    delCardList() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 处理 id
          const ids = this.selectedCardList.map((item) => item.id).join(",");
          await delCardAPI(ids);
          this.getCardList();
          this.$message({
            type: "success",
            message: "删除成功",
          });
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn {
    margin-left: 20px;
  }
}
.create-container {
  display: flex;
  justify-content: space-between;
  margin: 10px 0px;
  .tip {
    background: #e6f7ff;
    height: 39px;
    width: 427px;
    border: 1px solid #0094ff;
    border-radius: 3px;
    line-height: 39px;
    padding-left: 15px;
    i {
      color: #0094ff;
      margin-right: 10px;
    }
    span {
      font-size: 13px;
    }
  }
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
